package com.yaojiaxun.logisticsPlatformSystem.controller;

import com.yaojiaxun.logisticsPlatformSystem.dto.PageDTO;
import com.yaojiaxun.logisticsPlatformSystem.dto.RouteInfoDTO;
import com.yaojiaxun.logisticsPlatformSystem.entity.RouteInfo;
import com.yaojiaxun.logisticsPlatformSystem.entity.RouteSearch;
import com.yaojiaxun.logisticsPlatformSystem.service.RouteInfoService;
import com.yaojiaxun.logisticsPlatformSystem.util.ResultVOUtil;
import com.yaojiaxun.logisticsPlatformSystem.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @Auther: yjx_j
 * @Date: 2019/1/28 13:54
 * @Description:
 */
@Controller
public class RouteInfoController {

    @Autowired
    RouteInfoService routeInfoService;

    @RequestMapping("/route_manage")
    public String RouteManageIndex(@RequestParam(defaultValue = "1") Integer page, Map<String, Object> map){
        map.put("pageDTO", routeInfoService.findPage(page));
        return "system/route/route_manage";
    }

    @RequestMapping("/route_detail")
    public String getDetail(@RequestParam("routeId") Long routeId, @RequestParam("page") Integer page, Map<String, Object> map){
        map.put("page" ,page);
        map.put("routeInfo", routeInfoService.findRouteInfo(routeId));
        return "system/route/route_detail";
    }

    @RequestMapping("/route_update_after")
    public String updateRouteAfter(@RequestParam("page") Integer page, RouteInfo routeInfo){
        routeInfoService.updateRouteInfo(routeInfo);
        return "redirect:/route_manage?page=" + page;
    }

    @RequestMapping("/routeDelete")
    public String deleteRouteByRouteId(@RequestParam("routeId") Long routeId, @RequestParam("page") Integer page){
        routeInfoService.deleteRouteInfo(routeId);
        return "redirect:/route_manage?page=" + page;
    }

    @RequestMapping("/routeAddBefore")
    public String addRouteBefore(){
        return "system/route/route_add";
    }

    @RequestMapping("/route_add_after")
    public String addRouteAfter(RouteInfo routeInfo){
        routeInfoService.addRouteInfo(routeInfo);
        return "redirect:/route_manage";
    }

    //根据公司名称获取该公司的所有物流专线Json
    @ResponseBody
    @RequestMapping("/route_company_detail_json")
    public ResultVO findRouteByCompanyJson(@RequestParam("companyName") String companyName){
        return ResultVOUtil.success(routeInfoService.findRouteByCompany(companyName));
    }

    @RequestMapping("/route_list_search")
    public String findRouteByRAndDSearch(@RequestParam(defaultValue = "1") Integer page, @RequestParam("rProvince") String rProvince,@RequestParam("rCity") String rCity,@RequestParam("rCountry") String rCountry,
                                         @RequestParam("dProvince") String dProvince,@RequestParam("dCity") String dCity,@RequestParam("dCountry") String dCountry,Map<String,Object> map,RouteSearch routeSearch){
        routeSearch.setRProvince(rProvince);
        routeSearch.setRCity(rCity);
        routeSearch.setRCountry(rCountry);
        routeSearch.setDProvince(dProvince);
        routeSearch.setDCity(dCity);
        routeSearch.setDCountry(dCountry);
        PageDTO<RouteInfoDTO> pageDTO = routeInfoService.findPage(page, routeSearch);
        map.put("rProvince", rProvince);
        map.put("rCity", rCity);
        map.put("rCountry", rCountry);
        map.put("dProvince", dProvince);
        map.put("dCity", dCity);
        map.put("dCountry", dCountry);
        map.put("pageDTO", pageDTO);
        return "front/route/route_list_search";
    }

//    //发货单填写，先登录
//    @RequestMapping("/route_send_before")
//    public String routeSendBefore(@RequestParam("routeId") Long routeId, Map<String, Object> map, HttpSession session){
//        if (session.getAttribute("user") == null){
//            return "front/log/log_login";
//        }
//        return "front/route/route_send_before";
//    }
}
